/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.toolkit;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.swing.DefaultListModel;

/**
 *
 * @author Mido
 */
public class Toolkit {

    private static String lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));
    private static final DefaultListModel fontModel = new DefaultListModel();

    /**
     * Gets a list of all the fonts supported by the OS and adds them in the
     * font list
     */
    static {
        GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
        // get all system fonts
        final Font[] fonts = gEnv.getAllFonts();
        // intialize the font jlist
        for (int i = 0; i < fonts.length; i++) {
            fontModel.addElement(fonts[i].getName());
        }
    }

    /**
     * Return the line separator as a string
     *
     * @return
     */
    public static String getLineSeparator() {
        return lineSeparator;
    }

    /**
     * Returns a String representation of a stackTrace
     *
     * @param throwable the object to convert to string
     * @return
     */
    public static String getStackTraceAsString(Throwable throwable) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        throwable.printStackTrace(printWriter);
        return writer.toString();
    }

    /**
     * Returns details about the Operating system
     *
     * @return
     */
    public static String getOSDetails() {
        String details = "OS Name: " + System.getProperty("os.name") + ", ";
        details += "OS Version: " + System.getProperty("os.version") + ", ";
        details += "OS Architecture: " + System.getProperty("os.arch") + ", ";
        details += "Java Version: " + System.getProperty("java.version");
        return details;
    }

    /**
     * Aligns a given window to the center of the screen
     *
     * @param window the window to be aligned
     * @param height the height of the window
     * @param width the width of the window
     */
    public static void centerWindow(Window window, int height, int width) {
        // get the screen resolution
        Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        window.setLocation((screen.width - width) / 2, (screen.height - height) / 2);
    }

    /**
     * Return the physical path to the font name passed
     *
     * @param font_name
     * @returnW
     */
    public static String getFontPath(String font_name) {
        return "";//FontManager.getFontPath(true) + "/" + FontManager.getFileNameForFontName(font_name);
    }

    /**
     * Returns the font model containing all the fonts specified by the OS
     *
     * @return
     */
    public static DefaultListModel getFontModel() {
        return fontModel;
    }
}
